package com.sicheng.metadata.juc.线程通信.synchronize;

public class Share {
    private int number = 0;

    public synchronized void incr() {
        // 判断
        while (number != 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 干活
        System.out.println(Thread.currentThread().getName() + "::" + ++number);
        // 通知
        notifyAll();
    }

    public synchronized void decr() {
        // 判断
        while (number != 1) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 干活
        System.out.println(Thread.currentThread().getName() + "::" + --number);
        // 通知
        notifyAll();
    }
}
